// video/pages/goodsVideo/goodsVideo.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        videoList: [],
        showkey: 0,
        videoList: [],
        id: 0,
        common: 1,
        videoAlert: false,
        video_shopping_set: {},
        showProgress: false,
        play: "play"
    },
    onPlay(e) {},
    onPause(e) {
        //  console.log('pause', e.detail.activeId)
    },
    onEnded(e) {},
    onError(e) {},
    onWaiting(e) {},
    onTimeUpdate(e) {},
    onProgress(e) {},
    onLoadedMetaData(e) {},
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad(options) {
        this.getBase();
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        if (options.id) {
            this.setData({
                id: options.id
            });
        }
        this.getList(3, "");
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage() {},
    getList(comeon, direction) {
        let that = this;
        common.request({
            endpoint: "VideoShopping/manage/video-list",
            method: "post",
            data: {
                id: that.data.id
            },
            success: res => {
                let videoList = res.extra;
                if (videoList.length == 0) {
                    wx.hideLoading();
                    wx.showModal({
                        title: "提示",
                        content: "暂无视频",
                        complete: res => {
                            wx.navigateBack();
                        }
                    });
                    return;
                }
                if (comeon < 3) {
                    var listA = that.data.videoList;
                    if (direction == "up") {
                        listA[comeon] = videoList[2];
                    } else {
                        listA[comeon] = videoList[0];
                    }
                    this.setData({
                        videoList: listA
                    });
                    return;
                }
                that.setData({
                    id: videoList.length > 1 ? videoList[1]["id"] : videoList[0]["id"],
                    currentIndex: videoList.length > 1 ? 1 : 0,
                    videoList: videoList
                });
                console.log(that.data.videoList,'videoList')
            }
        });
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    video_shopping_set: res.extra.video_shopping_set,
                    showProgress: res.extra.video_shopping_set.status == 1
                });
                if (res.extra.video_shopping_set.status == 1) {
                    setTimeout(() => {
                        that.receive();
                        that.setData({
                            play: "pause"
                        });
                    }, parseInt(that.data.video_shopping_set.second) * 1e3);
                }
            }
        });
    },
    bindChangeSwiper: function(e) {
        let that = this;
        this.setData({
            id: e.detail.activeId
        });
        if (this.data.videoList.length >= 3) {
            this.getList(e.detail.changeC, e.detail.direction);
        }
    },
    receive: function() {
        var that = this;
        common.request({
            endpoint: "VideoShopping/manage/receive",
            method: "get",
            data: {},
            success: res => {
                if (res.code == 0) {
                    that.setData({
                        videoAlert: true
                    });
                    if (parseInt(that.data.video_shopping_set.limit) == res.extra.received_times) {
                        that.setData({
                            showProgress: false
                        });
                    }
                } else {
                    that.setData({
                        showProgress: false
                    });
                }
                // that.setData({
                //   video_shopping_set:res.extra.video_shopping_set
                // })
                // setTimeout(() => {
                //   that.setData({
                //     videoAlert:true
                //   })
                // },parseInt(that.data.video_shopping_set.second)*1000);
                        },
            fail: res => {
                that.setData({
                    showProgress: false
                });
            }
        });
    },
    back() {
        wx.navigateBack(-1);
    },
    close: function() {
        var that = this;
        that.setData({
            videoAlert: false,
            play: "play"
        });
        setTimeout(() => {
            that.receive();
            that.setData({
                play: "pause"
            });
        }, parseInt(that.data.video_shopping_set.second) * 1e3);
    },
    toDetail(e) {
        wx.navigateTo({
            url: `/pages/pro_detail/pro_detail?id=${this.data.id}`
        });
    }
});